package com.ruoyi.kanban.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kanban.domain.SupplyFramework;
import com.ruoyi.kanban.mapper.SupplyFrameworkMapper;
import com.ruoyi.kanban.service.ISupplyFrameworkService;

@Service
public class SupplyFrameworkServiceImpl extends ServiceImpl<SupplyFrameworkMapper, SupplyFramework>
        implements ISupplyFrameworkService {

    @Override
    public List<SupplyFramework> selectSupplyFrameworkList(SupplyFramework query) {
        LambdaQueryWrapper<SupplyFramework> wrapper = buildWrapper(query);
        return this.list(wrapper);
    }

    @Override
    public List<SupplyFramework> selectValidSupplyFrameworkList(SupplyFramework query) {
        LambdaQueryWrapper<SupplyFramework> wrapper = buildWrapper(query);
        // 添加有效性过滤条件
        wrapper.eq(SupplyFramework::getStatus, 0); // 状态为生效（0表示生效状态）
        wrapper.le(SupplyFramework::getBusinessStartDate, new java.util.Date()); // 业务开始日期 <= 当前日期
        wrapper.ge(SupplyFramework::getBusinessEndDate, new java.util.Date()); // 业务结束日期 >= 当前日期
        return this.list(wrapper);
    }

    @Override
    public SupplyFramework selectSupplyFrameworkById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean insertSupplyFramework(SupplyFramework entity) {
        return this.save(entity);
    }

    @Override
    public boolean updateSupplyFramework(SupplyFramework entity) {
        return this.updateById(entity);
    }

    @Override
    public boolean deleteSupplyFrameworkByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }

    private LambdaQueryWrapper<SupplyFramework> buildWrapper(SupplyFramework q) {
        LambdaQueryWrapper<SupplyFramework> w = new LambdaQueryWrapper<>();
        
        // 模糊搜索
        // if (StringUtils.isNotEmpty(q.getSearchValue())) {
        //     w.and(wrapper -> wrapper
        //         .like(SupplyFramework::getFrameworkCode, q.getSearchValue())
        //         .or().like(SupplyFramework::getReceiverCompanyName, q.getSearchValue())
        //         .or().like(SupplyFramework::getSenderCompanyName, q.getSearchValue())
        //         .or().like(SupplyFramework::getReceiverDeptName, q.getSearchValue())
        //         .or().like(SupplyFramework::getSenderDeptName, q.getSearchValue())
        //         .or().like(SupplyFramework::getReceiverWarehouseName, q.getSearchValue())
        //         .or().like(SupplyFramework::getSenderWarehouseName, q.getSearchValue())
        //         .or().like(SupplyFramework::getReceiverContact, q.getSearchValue())
        //         .or().like(SupplyFramework::getSenderContact, q.getSearchValue())
        //     );
        // }
        
        // 具体字段查询
        if (StringUtils.isNotEmpty(q.getFrameworkCode())) {
            w.like(SupplyFramework::getFrameworkCode, q.getFrameworkCode());
        }
        
        // 收货公司筛选 - 支持ID和名称
        if (q.getReceiverCompanyId() != null) {
            w.eq(SupplyFramework::getReceiverCompanyId, q.getReceiverCompanyId());
        }
        if (StringUtils.isNotEmpty(q.getReceiverCompanyName())) {
            w.like(SupplyFramework::getReceiverCompanyName, q.getReceiverCompanyName());
        }
        
        // 发货公司筛选 - 支持ID和名称
        if (q.getSenderCompanyId() != null) {
            w.eq(SupplyFramework::getSenderCompanyId, q.getSenderCompanyId());
        }
        if (StringUtils.isNotEmpty(q.getSenderCompanyName())) {
            w.like(SupplyFramework::getSenderCompanyName, q.getSenderCompanyName());
        }
        
        // 状态筛选
        if (q.getStatus() != null) {
            w.eq(SupplyFramework::getStatus, q.getStatus());
        }
        
        // 业务时间范围筛选
        if (q.getBusinessStartDate() != null) {
            w.ge(SupplyFramework::getBusinessStartDate, q.getBusinessStartDate());
        }
        if (q.getBusinessEndDate() != null) {
            w.le(SupplyFramework::getBusinessEndDate, q.getBusinessEndDate());
        }
        
        // 创建时间范围筛选 - 从params中获取
        if (q.getParams() != null) {
            Object beginCreateTime = q.getParams().get("beginCreateTime");
            Object endCreateTime = q.getParams().get("endCreateTime");
            
            if (beginCreateTime != null && StringUtils.isNotEmpty(beginCreateTime.toString())) {
                w.ge(SupplyFramework::getCreateTime, beginCreateTime.toString() + " 00:00:00");
            }
            if (endCreateTime != null && StringUtils.isNotEmpty(endCreateTime.toString())) {
                w.le(SupplyFramework::getCreateTime, endCreateTime.toString() + " 23:59:59");
            }
            
            // 业务日期范围筛选 - 从params中获取
            Object beginBusinessDate = q.getParams().get("beginBusinessDate");
            Object endBusinessDate = q.getParams().get("endBusinessDate");
            
            if (beginBusinessDate != null && StringUtils.isNotEmpty(beginBusinessDate.toString())) {
                w.ge(SupplyFramework::getBusinessStartDate, beginBusinessDate.toString());
            }
            if (endBusinessDate != null && StringUtils.isNotEmpty(endBusinessDate.toString())) {
                w.le(SupplyFramework::getBusinessEndDate, endBusinessDate.toString());
            }
        }
        
        // 默认按创建时间倒序排列
        w.orderByDesc(SupplyFramework::getCreateTime);
        
        return w;
    }
}